package com.are.odisea;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class ManejadorPermisosUsuNot {
	private db conexion = null;
	private PermisosUsuNot pu;
	
	public static final int ORDER_USU = 1;
	public static final int ORDER_DOC = 2;
	
	public ManejadorPermisosUsuNot(db conexion) {
		super();
		this.setConexion(conexion);
		this.setPu(new PermisosUsuNot());
	}
	public ManejadorPermisosUsuNot(db conexion, PermisosUsuNot pu) {
		super();
		this.setConexion(conexion);
		this.setPu(pu);
	}
	
	public db getConexion() {
		return conexion;
	}
	public void setConexion(db conexion) {
		this.conexion = conexion;
	}
	public PermisosUsuNot getPu() {
		return pu;
	}
	public void setPu(PermisosUsuNot pu) {
		this.pu = pu;
	}
	
	public boolean add() throws SQLException{
		boolean result = false;
			String sql = "insert into permisos_noticias (noticias_id,usuarios_id) values (?,?)";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1, pu.getIdnot());
			pst.setString(2, pu.getUsuario());
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
		return result;
	}
	public boolean update(String keyU, String keyD) throws SQLException{
		boolean result = false;

			String sql = "update permisos_noticias set noticias_id= ?, usuarios_id=? where noticias_id=? and usuarios_id=?";
			
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1, pu.getIdnot());
			pst.setString(2, pu.getUsuario());
			pst.setString(3, keyU);
			pst.setString(4, keyD);
			
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
			
		return result;
	}
	public boolean remove(String keyU, String keyD) throws SQLException {
		boolean result = false;

			String sql = "delete from permisos_noticias where usuarios_id=? and noticias_id=?";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1,keyU);
			pst.setString(2,keyD);
			if (conexion.Update(pst) > 0) {
				conexion.Commit();
				result = true;
			}
		return result;	
	}
	
	public boolean exist(String keyU, String keyD) throws SQLException {
		boolean result = false;

			String sql = "select * from permisos_noticias where usuarios_id=? and noticias_id=?";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1,keyU);
			pst.setString(2,keyD);
			ResultSet rs = conexion.Query(pst);
			if (rs.next()) {
				result = true;
			}
			rs.close();
		return result;	
	}
	
	public boolean Find(String key, String key2) throws SQLException {
		boolean result = false;

			String sql = "select * from vpermisos_noticias where usuarios_id=? and noticias_id=?";
			java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
			pst.setString(1,key);
			ResultSet rs = conexion.Query(pst);
			if (rs.next()) {
				pu = new PermisosUsuNot();
				pu.setUsuario(rs.getString("user"));
				pu.setIdnot(rs.getString("id_not"));
				pu.setNomDoc(rs.getString("titnot"));
				pu.setNomusuario(rs.getString("nomusuario"));
				result = true;
			}
			rs.close();
		return result;	
	}
	
	public ArrayList<PermisosUsuNot> list(int orden, String Id_Doc) throws SQLException {
		ArrayList<PermisosUsuNot> pr = new ArrayList<PermisosUsuNot>();
		String sql ="";
		if(Id_Doc.equals("")){
				 sql = "select * from vpermisos_noticias";
			}else{
				 sql = "select * from vpermisos_noticias where id_not="+Id_Doc;
			}
			switch(orden) {
			case ORDER_USU:
				sql += " order by user";
			break;
			case ORDER_DOC:
				sql += " order by id_not";
			break;
			}
			ResultSet rs  = conexion.Query(sql);
			while(rs.next()) {
				PermisosUsuNot p = new PermisosUsuNot();
				p.setUsuario(rs.getString("user"));
				p.setIdnot(rs.getString("id_not"));
				p.setNomDoc(rs.getString("titnot"));
				p.setNomusuario(rs.getString("nomusuario"));
				pr.add(p);
			}
			rs.close();
		return pr;	
	}
}
